package com.liukai.todo_android.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;

import androidx.appcompat.widget.AppCompatButton;

import com.liukai.todo_android.R;
import com.liukai.todo_android.common.base.BaseFragment;
import com.liukai.todo_android.ui.activity.LoginActivity;
import com.liukai.todo_android.common.utils.Constants;
import com.liukai.todo_android.common.utils.State;

import butterknife.BindView;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/9 4:16 下午
 * @description：待办事项页面
 * @modified By：
 * @version: $
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.btn_logout)
    AppCompatButton btnLogout;

    private Context context;

    @Override
    protected void initDate(View view) {
        setUpState(State.SUCCESS);
    }

    @Override
    protected Context getFragmentContext(View view) {
        context = view.getContext();
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
    }

    @Override
    protected void initAdapter(View view) {
        super.initAdapter(view);
    }

    @Override
    protected void initListener() {
        super.initListener();
        btnLogout.setOnClickListener(v -> {
            removeToken();
            removeUserId();
            startActivity(new Intent(context, LoginActivity.class));
            MineFragment.this.getActivity().finish();
        });
    }

    private void removeToken() {
        SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARE_NAME_TOKEN, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(Constants.TOKEN_KEY);
        editor.commit();
    }

    private void removeUserId() {
        SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARE_NAME_USER, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(Constants.USER_ID_KEY);
        editor.commit();
    }

    @Override
    protected void release() {
        super.release();
    }

}
